home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet multimedia / Animacje, filmy i prezentacje / Modelowanie 3D / K-3D 0.6.5.0 / k3d-all-in-one-setup-0.6.5.0.exe / k3d-setup-0.6.5.0.exe / share / shaders / k3d_km.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-19  |  2.2 KB  |  85 lines

  1. /* 
  2.  * km.h
  3.  * 
  4.  * Copyright (C) 2001, Matt Pharr <mmp@Exluna.com> 
  5.  * 
  6.  * This software is placed in the public domain and is provided as is 
  7.  * without express or implied warranty. 
  8.  *
  9.  * Basic implementation of the Kubelka-Munk model for the composition
  10.  * of layered surfaces.  See SIGGRAPH 2001 course notes, "Advanced 
  11.  * RenderMan 3: Render Harder," for notes and background information.
  12.  */
  13.  
  14. void sinhcosh(float t; output float sinh, cosh;) {
  15.     if (t > 10) t = 10.;
  16.     float ex = exp(t);
  17.     float invex = 1. / ex;
  18.  
  19.     sinh = 0.5 * (ex - invex);
  20.     cosh = 0.5 * (ex + invex);
  21. }
  22.  
  23.  
  24. void KMEstimateCoeffs(color Rinf; output color sigma_a, sigma_s;) {
  25.     if (Rinf == color 0.) {
  26.         sigma_a = color 1.;
  27.         sigma_s = color 0.;
  28.     }
  29.     else {
  30.         color a_over_s = (1. - Rinf) * (1. - Rinf) / (2. * Rinf);
  31.         sigma_a = color 1.;
  32.         sigma_s = sigma_a / a_over_s;
  33.     }
  34. }
  35.  
  36. void KM(color sigma_a, sigma_s; float thickness;
  37.         output color R, T;) {
  38.     float i;
  39.     for (i = 0; i < ncomps; i += 1) {
  40.         float s = comp(sigma_s, i), a = comp(sigma_a, i);
  41.         float aa = (s+a)/s;
  42.         float b = sqrt(max(aa*aa - 1., 0.));
  43.  
  44.         float sh, ch;
  45.         sinhcosh(b*s*thickness, sh, ch);
  46.  
  47.         setcomp(R, i, sh / (aa * sh + b * ch));
  48.         setcomp(T, i, b  / (aa * sh + b * ch));
  49.     }
  50. }
  51.  
  52. color KMInfinite(color sigma_a, sigma_s;) {
  53.     float i;
  54.     color R = 0;
  55.     for (i = 0; i < ncomps; i += 1) {
  56.         float a = comp(sigma_a, i) / comp(sigma_s, i);
  57.         float r = 1. + a - sqrt(a*a + 2.*a);
  58.         setcomp(R, i, r);
  59.     }
  60.     return R;
  61. }
  62.  
  63. color KMComposeR(color R1, T1, R2, T2) {
  64.     return R1 + T1*R2*T1 / (color 1. - R1*R2);
  65. }
  66.  
  67. color KMComposeT(color R1, T1, R2, T2) {
  68.     return T1*T2 / (color 1. - R1*R2);
  69. }
  70.  
  71. color KMOverGlossy(normal Nf; color sigma_a, sigma_s, Kd, Ks;
  72.                    float thickness, roughness;) {
  73.     color R1, T1;
  74.     KM(sigma_a, sigma_s, thickness, R1, T1);
  75.  
  76.     color Kdnew = KMComposeR(R1, T1, Kd, color 0.);
  77.     color Ksnew = KMComposeR(0., T1, Ks, color 0.);
  78.  
  79.     float KsRatio = comp(Ks, 0) / comp(Ksnew, 0);
  80.     float roughnew = roughness * KsRatio;
  81.  
  82.     extern vector I;
  83.     return Kdnew*diffuse(Nf) + Ksnew*specular(Nf, -normalize(I), roughnew);
  84. }
  85.